package br.com.eres.ui.adapters;

import java.text.DecimalFormat;
import java.util.List;

import br.com.carcara.scc.R;
import br.com.eres.model.ItemMesa;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


public class ItemMesaAdapter extends BaseAdapter {
	
	List<ItemMesa> itens;
	private LayoutInflater mInflater;
	private ViewHolder holder;
	
	static class ViewHolder {
		
		TextView txtDescricao;
		TextView txtPreco;
		TextView txtQuantidade;
		TextView txtValorTotal;
		TextView txtMultiplicador;
		TextView txtSequencial;
		
	}
	
	public ItemMesaAdapter(Context context, List<ItemMesa> itensMesa) {
		mInflater = LayoutInflater.from(context);
		this.itens = itensMesa;
	}

	@Override
	public int getCount() {
		return itens.size();
	}

	@Override
	public Object getItem(int position) {		
		return itens.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.lista_conferencia, null);
			holder = new ViewHolder();
 
			holder.txtDescricao = (TextView) convertView.findViewById(R.listaConferencia.txtDescricao);
			holder.txtQuantidade = (TextView) convertView.findViewById(R.listaConferencia.txtQuantidade);
			holder.txtPreco = (TextView) convertView.findViewById(R.listaConferencia.txtPreco);
			holder.txtValorTotal = (TextView) convertView.findViewById(R.listaConferencia.txtValorTotal);
			holder.txtMultiplicador = (TextView) convertView.findViewById(R.listaConferencia.txtMultiplicador);
			holder.txtSequencial = (TextView) convertView.findViewById(R.listaConferencia.txtSequencial);
 
			convertView.setTag(holder);
 
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
 
		DecimalFormat dft = new DecimalFormat("000");
		ItemMesa mItemMesa = itens.get(position);							
		
		holder.txtSequencial.setText(dft.format(position + 1));
		holder.txtDescricao.setText(mItemMesa.descricao);
		holder.txtQuantidade.setText(mItemMesa.quantidade);
		holder.txtPreco.setText(mItemMesa.preco);
		holder.txtValorTotal.setText(mItemMesa.precoTotal);
		
		
		if (holder.txtValorTotal.getText().toString().trim().contains("Cancelado")) {
			holder.txtValorTotal.setTextColor(Color.RED);
		} else {
			holder.txtValorTotal.setTextColor(Color.parseColor("#999999"));
		}
		
		return convertView;
	}
	
   

}
